package com.liao.work.ui.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

import com.liao.work.R;
import com.liao.work.ui.adapter.CarouseViewPagerAdapter;
import com.liao.work.util.SetActionBarTitle;

import java.util.ArrayList;

public class CarouselActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener {

    private ViewPager viewPager;
    private LinearLayout layout;

    private ArrayList<View> views;
    private int[] images = new int[]{R.mipmap.ab1, R.mipmap.ab2, R.mipmap.ab3, R.mipmap.ab4, R.mipmap.ab5};
    private int[] icons = new int[]{R.mipmap.a2, R.mipmap.a3};
    private ArrayList<ImageView> iconList;
    private CarouseViewPagerAdapter adapter;

    private boolean isRun = true;
    private int currImg = 0;
    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            viewPager.setCurrentItem(currImg);
            return false;
        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_carousel);
        SetActionBarTitle.setTitle(getSupportActionBar(), false, "轮播");
        initView();
        change();
    }

    private void initView() {
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        layout = (LinearLayout) findViewById(R.id.layout);

        views = new ArrayList<>();
        iconList = new ArrayList<>();
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        params.leftMargin = 10;
        for (int i = 0; i < images.length; i++) {
            ImageView imageView = new ImageView(this);
            imageView.setImageResource(images[i]);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            views.add(imageView);

            ImageView icon = new ImageView(this);
            if (i == 0) {
                icon.setImageResource(icons[1]);
            } else {
                icon.setImageResource(icons[0]);
            }
            icon.setLayoutParams(params);
            layout.addView(icon);
            iconList.add(icon);
        }
        adapter = new CarouseViewPagerAdapter(views);
        viewPager.setAdapter(adapter);
        viewPager.addOnPageChangeListener(this);
    }

    private void change() {
        new Thread() {
            @Override
            public void run() {
                while (isRun) {
                    try {
                        sleep(1500);
                        int currentItem = viewPager.getCurrentItem();
                        currImg = currentItem >= views.size() - 1 ? 0 : currentItem + 1;
                        handler.sendEmptyMessage(0x001);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        for (int i = 0; i < iconList.size(); i++) {
            if (i == position) {
                iconList.get(i).setImageResource(icons[1]);
            } else {
                iconList.get(i).setImageResource(icons[0]);
            }
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        isRun = false;
    }
}
